<?php

require_once('./discuz_publish.php');
set_time_limit(0);

function get_url($url, $base_url='')
{
	$content = file_get_contents($url);
	file_put_contents( dirname(__FILE__ ) . '/log', $content);
	preg_match_all('/<td height="24"><img src="\/cn\/UpFiles\/arrow_list1.gif" border="0"\/>.*?<\/span><\/td>/', $content, $matches);
	$matches = $matches[0];
	if(is_array($matches))
	{
		foreach($matches as $v)
		{
			$v = substr($v, stripos($v, 'href="')+strlen('href="'));
			$href = $base_url . substr($v, 0, stripos($v, '"'));

			$v = substr($v, stripos($v, 'title="')+strlen('title="'));
			$title = substr($v, 0, stripos($v, '"'));
			$result[] = array('href'=>$href, 'title'=>$title);
		}
	}

	return $result;
	//file_put_contents( dirname(__FILE__ ) . '/log', var_export($result, true));
}

$base_url = 'http://www.enmajor.com';
$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/Essays/index_2.html', 'http://www.enmajor.com/cn/Html/Listening_Speaking/Essays/index_3.html');

if($_GET['i'] == 1)
{
	$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/English_Writing/', 'http://www.enmajor.com/cn/Html/Listening_Speaking/English_Writing/index_2.html');
}
else if($_GET['i'] == 2)
{
	$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/Oral_English/', 'http://www.enmajor.com/cn/Html/Listening_Speaking/Oral_English/index_2.html', 'http://www.enmajor.com/cn/Html/Listening_Speaking/Oral_English/index_3.html', 'http://www.enmajor.com/cn/Html/Listening_Speaking/Oral_English/index_4.html');	
}
else if($_GET['i'] == 3)
{
	$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/Listening/index.html', 'http://www.enmajor.com/cn/Html/Listening_Speaking/Listening/index_2.html','http://www.enmajor.com/cn/Html/Listening_Speaking/Listening/index_3.html');
}
else
{
	$urls = array('http://www.enmajor.com/cn/Html/Listening_Speaking/jokes/');
}

$i = 0;
foreach($urls as $url)
{
	$result = get_url($url, $base_url);
	foreach($result as $v)
	{
		$i++;
		if($i < 80) continue;
		
		get_content($v['href'], $v['title']);
		if($i == 300) exit(0);
	}
}

exit(0);

function get_content($href, $title)
{
	$base_url = $href;
	$content = file_get_contents($base_url);
	file_put_contents( dirname(__FILE__ ) . '/log', $content . '{br}');//exit(0);
	$i = 0;

	$content = file_get_contents($base_url);
	
	$pos = stripos($content, 'language="javascript"></script> </div>');
	$content = substr($content, $pos + strlen('language="javascript"></script> </div>'));
	
	if( $pos == false)
	{
		$content = substr($content, stripos($content, '<table border="0" cellspacing="5" cellpadding="5" width="100%">') + strlen('<table border="0" cellspacing="5" cellpadding="5" width="100%">'));
	}
	$cont = substr($content, 0, stripos($content, "<center>"));
		
	if(empty($title)) break;

	$title = iconv('gbk', 'utf-8', $title);
	$cont = iconv('gbk', 'utf-8', $cont);

	file_put_contents( dirname(__FILE__ ) . '/log', $title . '{br}'. $cont . '{br}');
//exit(0);
	discuz_publish(escape_s($title), escape_s($cont), 78);
//EXIT(0);
	//file_put_contents( dirname(__FILE__ ) . '/log', escape_s($title) . '{br}'. escape_s($cont) . '{br}', FILE_APPEND);
	//exit(0);
}

function escape_s($content)
{
	$content = str_replace( "'", "\'", $content);
	$content = str_replace('"', '\"', $content);
	$content = str_replace('<br />', '', $content);
	$content = preg_replace('/<div.*?>/i', '', $content);
	$content = preg_replace('/<\/div>/i', '', $content);
	$content = preg_replace('/<p>/i', "\r\n", $content);
	return $content;
}
//var_dump($next);




?>